import asyncio
import logging
from bleak import BleakClient
import python_aida64_sample
import time

address = "96:99:66:66:99:99"
MODEL_NBR_UUID = "00002a24-0000-1000-8000-00805f9b34fb"
server_uuid =  "F000FFF2-0451-4000-B000-000000000000"

size = 16

async def run(address):
    async with BleakClient(address) as client:
        test = python_aida64_sample.Aida64_Basic_Data()
        test.get_datas()
        print(test.RTSSFPS)
        x = await client.is_connected()
        print(x)

        # await client.setMTU(200)

        while True:
          test.get_datas()
        # await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", bytearray(['1', '2']))
          # sb = bytes("LCD:FPS:" + test.RTSSFPS + "  ", encoding='utf-8')
          # print(sb)
          lcd_head_bytes = bytes("LCD:", encoding='utf-8')
          char_split_byte = bytes(",", encoding='utf-8')

          x_pos = 0
          x_pos_bytes = x_pos.to_bytes(2, byteorder="little")
          y_pos = 0
          y_pos_bytes = y_pos.to_bytes(2, byteorder="little")
          f_color = 0xFFFF
          f_color_bytes = f_color.to_bytes(2, byteorder="little")
          b_color = 0
          b_color_bytes = b_color.to_bytes(2, byteorder="little")
         
          data_size = size
          data_size_bytes = data_size.to_bytes(2, byteorder="little")

          send_bytes = lcd_head_bytes + x_pos_bytes + char_split_byte + y_pos_bytes + char_split_byte + f_color_bytes + char_split_byte + b_color_bytes + char_split_byte + bytes("CPUCLCK" + ":" + test.CPUCLCK + "  ", encoding='utf-8') + char_split_byte + data_size_bytes

          # print(send_bytes)
          await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", send_bytes)
          time.sleep(0.1)

          ###################################################################################################
          # x_pos = 0
          # x_pos_bytes = x_pos.to_bytes(2, byteorder="little")
          y_pos = y_pos + size
          y_pos_bytes = y_pos.to_bytes(2, byteorder="little")
          # f_color = 0xFFFF
          # f_color_bytes = f_color.to_bytes(2, byteorder="little")
          # b_color = 0
          # b_color_bytes = b_color.to_bytes(2, byteorder="little")
         
          # data_size = size
          # data_size_bytes = data_size.to_bytes(2, byteorder="little")

          send_bytes = lcd_head_bytes + x_pos_bytes + char_split_byte + y_pos_bytes + char_split_byte + f_color_bytes + char_split_byte + b_color_bytes + char_split_byte + bytes("CPUUTI" + ":"  + test.CPUUTI + "  ", encoding='utf-8') + char_split_byte + data_size_bytes

          # print(send_bytes)
          await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", send_bytes)
          time.sleep(0.1)

          ###################################################################################################

          # x_pos = 0
          # x_pos_bytes = x_pos.to_bytes(2, byteorder="little")
          y_pos = y_pos + size
          y_pos_bytes = y_pos.to_bytes(2, byteorder="little")
          # f_color = 0xFFFF
          # f_color_bytes = f_color.to_bytes(2, byteorder="little")
          # b_color = 0
          # b_color_bytes = b_color.to_bytes(2, byteorder="little")
         
          # data_size = size
          # data_size_bytes = data_size.to_bytes(2, byteorder="little")

          send_bytes = lcd_head_bytes + x_pos_bytes + char_split_byte + y_pos_bytes + char_split_byte + f_color_bytes + char_split_byte + b_color_bytes + char_split_byte + bytes("MEMUTI" + ":"  + test.MEMUTI + "  ", encoding='utf-8') + char_split_byte + data_size_bytes

          # print(send_bytes)
          await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", send_bytes)
          time.sleep(0.1)

          ###################################################################################################

          # x_pos = 0
          # x_pos_bytes = x_pos.to_bytes(2, byteorder="little")
          y_pos = y_pos + size
          y_pos_bytes = y_pos.to_bytes(2, byteorder="little")
          # f_color = 0xFFFF
          # f_color_bytes = f_color.to_bytes(2, byteorder="little")
          # b_color = 0
          # b_color_bytes = b_color.to_bytes(2, byteorder="little")
         
          # data_size = size
          # data_size_bytes = data_size.to_bytes(2, byteorder="little")

          send_bytes = lcd_head_bytes + x_pos_bytes + char_split_byte + y_pos_bytes + char_split_byte + f_color_bytes + char_split_byte + b_color_bytes + char_split_byte + bytes("GPU1UTI" + ":"  + test.GPU1UTI + "  ", encoding='utf-8') + char_split_byte + data_size_bytes

          # print(send_bytes)
          await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", send_bytes)
          time.sleep(0.1)

          ###################################################################################################

          # x_pos = 0
          # x_pos_bytes = x_pos.to_bytes(2, byteorder="little")
          y_pos = y_pos + size
          y_pos_bytes = y_pos.to_bytes(2, byteorder="little")
          # f_color = 0xFFFF
          # f_color_bytes = f_color.to_bytes(2, byteorder="little")
          # b_color = 0
          # b_color_bytes = b_color.to_bytes(2, byteorder="little")
         
          # data_size = size
          # data_size_bytes = data_size.to_bytes(2, byteorder="little")

          send_bytes = lcd_head_bytes + x_pos_bytes + char_split_byte + y_pos_bytes + char_split_byte + f_color_bytes + char_split_byte + b_color_bytes + char_split_byte + bytes("RTSSFPS" + ":"  + test.RTSSFPS + "  ", encoding='utf-8') + char_split_byte + data_size_bytes

          # print(send_bytes)
          await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", send_bytes)
          time.sleep(0.1)

          ###################################################################################################

          # x_pos = 0
          # x_pos_bytes = x_pos.to_bytes(2, byteorder="little")
          y_pos = y_pos + size
          y_pos_bytes = y_pos.to_bytes(2, byteorder="little")
          # f_color = 0xFFFF
          # f_color_bytes = f_color.to_bytes(2, byteorder="little")
          # b_color = 0
          # b_color_bytes = b_color.to_bytes(2, byteorder="little")
         
          # data_size = size
          # data_size_bytes = data_size.to_bytes(2, byteorder="little")

          send_bytes = lcd_head_bytes + x_pos_bytes + char_split_byte + y_pos_bytes + char_split_byte + f_color_bytes + char_split_byte + b_color_bytes + char_split_byte + bytes("CPUTEMP" + ":"  + test.CPUTEMP + "  ", encoding='utf-8') + char_split_byte + data_size_bytes

          # print(send_bytes)
          await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", send_bytes)
          time.sleep(0.1)

          ###################################################################################################

          
          # x_pos = 0
          # x_pos_bytes = x_pos.to_bytes(2, byteorder="little")
          y_pos = y_pos + size
          y_pos_bytes = y_pos.to_bytes(2, byteorder="little")
          # f_color = 0xFFFF
          # f_color_bytes = f_color.to_bytes(2, byteorder="little")
          # b_color = 0
          # b_color_bytes = b_color.to_bytes(2, byteorder="little")
         
          # data_size = size
          # data_size_bytes = data_size.to_bytes(2, byteorder="little")

          send_bytes = lcd_head_bytes + x_pos_bytes + char_split_byte + y_pos_bytes + char_split_byte + f_color_bytes + char_split_byte + b_color_bytes + char_split_byte + bytes("GPUTEMP" + ":"  + test.GPUTEMP + "  ", encoding='utf-8') + char_split_byte + data_size_bytes

          # print(send_bytes)
          await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", send_bytes)
          time.sleep(0.1)

          ###################################################################################################


      # sb = 1
      # print(sb)
      # await client.write_gatt_char("f000fff1-0451-4000-b000-000000000000", sb)



    	# for service in client.services:
    	# 	# print(service)
    	# 	for char in service.characteristics:
    	# 		# print(char)
    	# 		if "write" in char.properties:
    	# 			print(char.uuid)
    	# 			await client.write_gatt_char(char.uuid, "1234")
    				# try:
    					# await client.
  #   	x = await client.is_connected()
  #       # log.info("Connected: {0}".format(x))

		# for service in client.services:
  #           log.info("[Service] {0}: {1}".format(service.uuid, service.description))
  #           for char in service.characteristics:
  #               if "read" in char.properties:
  #                   try:
  #                       value = bytes(await client.read_gatt_char(char.uuid))
  #                   except Exception as e:
  #                       value = str(e).encode()
  #               else:
  #                   value = None
  #               log.info(
  #                   "\t[Characteristic] {0}: (Handle: {1}) ({2}) | Name: {3}, Value: {4} ".format(
  #                       char.uuid,
  #                       char.handle,
  #                       ",".join(char.properties),
  #                       char.description,
  #                       value,
  #                   )
  #               )
  #               for descriptor in char.descriptors:
  #                   value = await client.read_gatt_descriptor(descriptor.handle)
  #                   log.info(
  #                       "\t\t[Descriptor] {0}: (Handle: {1}) | Value: {2} ".format(
  #                           descriptor.uuid, descriptor.handle, bytes(value)
  #                       )
  #                   )
    	# print(client)
    	# services = await client.get_services()
    	# print(services)
    	# server_number = await client.read_gatt_char(server_uuid)
        # model_number = await client.read_gatt_char(MODEL_NBR_UUID)
        # print("Model Number: {0}".format("".join(map(chr, model_number))))
		#print(server_number)

loop = asyncio.get_event_loop()
loop.run_until_complete(run(address))